/**
 * 表单填报 列表页
 *
 * 作者 zhangwei
 * 日期 2024-09-05 08:53:07
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-formFill'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#formFill-table-form'),
        tableIns;

    form.verify(validate);
    form.render();
    initDate()
    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#laydate-range',
            range: ['#startDate', '#endDate'],
            format: 'yyyy-MM-dd',
            btns: ['now', 'confirm']
        })
        // $('#endDate').val(commonJS.getCurrentDateStr(new Date()));
        // $('#startDate').val(commonJS.getPastDateStr(new Date(), 7));
        form.render();

    }
    initTable();
    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'formFillRecordTable',
            url: ctx + 'formFillRecord/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'tenantName', title: '公司名称', minWidth: 260,hide:!hasHeadCompanyRole},
                {field: 'formName', title: '表单名称', minWidth: 120,event:'detail',templet:function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">'+d.formName+'</p>'
                    }},
                {field: 'startDate', title: '开始时间', width: 120},
                {field: 'endDate', title: '结束时间', width: 120},
                {field: 'status', title: '状态', width: 100,align: 'center',
                    templet:function (d){
                        if(d.status == 1){
                            return '待提交'
                        }else if(d.status == 2){
                            return '已提交'
                        }
                    }
                }
            ]],
            // toolbar: '#formFill-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(formFillRecordTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('tool(formFillRecordTable)', function (obj) {
        var data = obj.data,
            layEvent = obj.event;
        if (layEvent === 'detail') {
            febs.modal.open('表单录入详情', 'formFill/detail/' + data.registerId, {
                area: [$(window).width() <= 750 ? '95%':'50%','95%'],
                btn: ['关闭'],
                yes: function (index, layero) {
                    layer.closeAll();
                }
            });
        }
    });
    function getQueryParams() {
        return {
            formName: $searchForm.find('input[name="formName"]').val()?.trim(),
            tenantId: $searchForm.find("select[name='tenantId']").find("option:selected").val(),
            startDate: $searchForm.find('input[name="startDate"]').val() || '',
            endDate: $searchForm.find('input[name="endDate"]').val() || '',
            status: $searchForm.find("select[name='status']").find("option:selected").val(),
            invalidate_ie_cache: new Date()
        };
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});